"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下 查询pg_thread_wait_status信息
Create At   : 2024-11-22
Owner       : huzhengchao
Description :
    1、查询集群状态
    2、查看参数 ss_enable_dynamic_trace 
    3、查看 pg_thread_wait_status 内的mes线程数量
    4、关闭参数ss_enable_dynamic_trace 
    5、重启集群
    6、查看参数 ss_enable_dynamic_trace 
    7、查看pg_thread_wait_status内的mes线程数量
    8、打开参数ss_enable_dynamic_trace 
    9、恢复集群原始状态

Expect      :
    1、查询结果正常
    2、参数on
    3、能够看到相关的线程信息
    4、关闭参数成功
    5、重启集群成功，状态正常，数据库节点状态正常
    6、参数off
    7、不能够看到相关的线程信息
    8、打开参数成功
    9、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-11-22 创建
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        
        text = '-----step2:查看参数; expect:参数打开-----'
        self.log.info(text)
        sql = f"show ss_enable_dynamic_trace;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('off', status, '执行失败' + text)

        text = '-----step3:pg_thread_wait_status; expect:数据为0-----'
        self.log.info(text)
        sql = "select case when count(*) > 0 then 'yes' else 'no' end as have_dms_thread " + \
              "from pg_thread_wait_status " + \
              "where thread_name like 'mes%';"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('no', status, '执行失败：不应该查询的到dms相关线程。' + text)
        
        text = '-----step4:关闭参数 ss_enable_dynamic_trace; expect:关闭成功-----'
        result = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'ss_enable_dynamic_trace=on')
        self.assertTrue(result, '执行失败' + text)
        
        text = '-----step5:重启集群 expect:启动集群成功，集群状态正常，数据库节点状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        
        
        text = '-----step6:查看参数 ss_enable_dynamic_trace ; expect:参数都开启-----'
        self.log.info(text)
        sql = f"show ss_enable_dynamic_trace;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('on', status, '执行失败' + text)

        text = '-----step7:pg_thread_wait_status; expect:数据不为0-----'
        self.log.info(text)
        sql = "select case when count(*) > 0 then 'yes' else 'no' end as have_dms_thread " + \
              "from pg_thread_wait_status " + \
              "where thread_name like 'mes%';"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('yes', status, '执行失败：查询不到dms相关线程。' + text) 
        
    def tearDown(self):
        text = '-----恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        text = '-----恢复参数 ss_enable_dynamic_trace; expect:成功-----'
        result = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'ss_enable_dynamic_trace=off')
        self.assertTrue(result, '执行失败' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        sql = f"show ss_enable_dynamic_trace;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('off', status, '参数恢复失败' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
